package com.bjim.resource;

import java.util.List;

import com.bjim.AResult;
import com.bjim.model.Project;
import com.bjim.model.ProjectMember;
import com.bjim.model.Pwork;
import com.bjim.model.User;

import cn.dreampie.orm.Record;
import cn.dreampie.route.annotation.API;
import cn.dreampie.route.annotation.GET;

@API("p")
public class ProjectResource extends ApiResource {
	
	@GET("/all")
	public AResult findAll(){
		return new AResult(Project.dao.findAll());
	}
	
	@GET("/add")
	public AResult AddProject(String pname,String ptype,String industry,String resource,String phase,String level,String members){
		Project project=new Project().set("pname",pname).set("ptype",ptype).set("industry",industry).set("resource",resource).set("phase",phase).set("level",level).set("members",members);
		project.save();
		for(String member:members.split(",")){
			ProjectMember pm=new ProjectMember().set("pid",project.get("pid")).set("userid", member);
			pm.save();
		}
		return new AResult(findByPid(project.getInt("pid")));
	}
	
	@GET("/:pid/up")
	public AResult UpProject(Object pid,String pname,String ptype,String industry,String resource,String phase,String level,String members){
		Project project=Project.dao.findById(pid).set("pname",pname).set("ptype",ptype).set("industry",industry).set("resource",resource).set("phase",phase).set("level",level).set("members",members);
		project.save();
		return new AResult(project);
	}
	
	@GET("/:pid")
	public AResult findByPid(int pid){
		Project project=Project.dao.findById(pid);
		List<Record> members=new Record().find("select c.userid,c.username,b.startdate,b.enddate from ts_project a,ts_project_member b,ts_user c where a.pid=b.pid and b.userid=c.userid and a.pid=?",pid);
		project.put("members", members);
		return new AResult(project);
	}
	
	@GET("/type/list")
	public AResult ListTypes(){
		List<Record> records=new Record().find("select attrid,attrname from ts_class_attr where classcode='ptype'");
		return new AResult(records);
	} 
	@GET("/industry/list")
	public AResult ListIndustry(){
		List<Record> records=new Record().find("select attrid,attrname from ts_class_attr where classcode='industry'");
		return new AResult(records);
	} 
	@GET("/resource/list")
	public AResult ListResources(){
		List<Record> records=new Record().find("select attrid,attrname from ts_class_attr where classcode='resource'");
		return new AResult(records);
	} 
	@GET("/level/list")
	public AResult ListLevel(){
		List<Record> records=new Record().find("select attrid,attrname from ts_class_attr where classcode='level'");
		return new AResult(records);
	} 
	@GET("/phase/list")
	public AResult ListPhase(){
		List<Record> records=new Record().find("select attrid,attrname from ts_class_attr where classcode='phase'");
		return new AResult(records);
	} 
	
	@GET("/:pid/pwork/list")
	public AResult ListWorks(Object pid){
		List<Pwork> pworks=Pwork.dao.findBy("pid=?", pid);
		return new AResult(pworks);
	} 
	
	@GET("/:pid/pwork/add")
	public AResult ADDWork(int pid,String userid,String thisweek,String nextweek,String sourceneed){
		Pwork pwork=new Pwork().set("pid", pid).set("userid", userid).set("thisweek", thisweek).set("nextweek", nextweek).set("sourceneed", sourceneed);
		pwork.set("username", User.dao.findById(userid).get("username"));
		pwork.save();
		return new AResult(pwork);
	} 
	
}
